TOPDIR = $(shell pwd)

ARCH     = arm
ARCH_VER = armv4t
CPU      = arm920t
SOC      = s3c2440
VENDOR   = samsung
BOARD    = qt2440

ifeq ($(ARCH),arm)
ifeq ($(ARCH_VER),armv3)
    CPU_ARCH = 1
endif
ifeq ($(ARCH_VER),armv4)
    CPU_ARCH = 2
endif
ifeq ($(ARCH_VER),armv4t)
    CPU_ARCH = 3
endif
ifeq ($(ARCH_VER),armv5)
    CPU_ARCH = 4
endif
ifeq ($(ARCH_VER),armv5t)
    CPU_ARCH = 5
endif
ifeq ($(ARCH_VER),armv5te)
    CPU_ARCH = 6
endif
ifeq ($(ARCH_VER),armv5tej)
    CPU_ARCH = 7
endif
ifeq ($(ARCH_VER),armv6)
    CPU_ARCH = 8
endif
ifeq ($(ARCH_VER),armv7)
    CPU_ARCH = 9
endif
endif

CROSS_COMPILE = /media/DATA/arm-armv4t-linux-gnueabi/bin/arm-armv4t-linux-gnueabi-
#CROSS_COMPILE = /media/DATA/x-tools/arm-armv4t-linux-gnueabi/bin/arm-armv4t-linux-gnueabi-

export TOPDIR ARCH ARCH_VER CPU SOC VENDOR BOARD CROSS_COMPILE 

MAJOR_VER = 0
MINOR_VER = 1
MBOOT_VER = $(MAJOR_VER).$(MINOR_VER)

BUILD_YEAR  = $(shell date +%y)
BUILD_MONTH = $(shell date +%m)
BUILD_DAY   = $(shell date +%d)
BUILD_HOUR  = $(shell date +%H)
BUILD_MIN   = $(shell date +%M)
BUILD_SEC   = $(shell date +%S)

AS      = $(CROSS_COMPILE)as
CC      = $(CROSS_COMPILE)gcc
LD      = $(CROSS_COMPILE)ld
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy

CFLAGS = -I$(TOPDIR)/include -I$(TOPDIR)/include/uCOSIII -I$(TOPDIR)/include/arch/uCOSIII -ffreestanding -nostdinc -nostdlib -fno-builtin -mno-thumb-interwork  -DMBOOT_MAJOR_VER=$(MAJOR_VER) -DMBOOT_MINOR_VER=$(MINOR_VER) -m$(ARCH) -march=$(ARCH_VER) -mabi=aapcs-linux -Wall -Wstrict-prototypes -fomit-frame-pointer -O2 

CFLAGS += -DBUILD_TIME=\"$(BUILD_YEAR)-$(BUILD_MONTH)-$(BUILD_DAY)\ $(BUILD_HOUR):$(BUILD_MIN):$(BUILD_SEC)\"
CFLAGS += -D__MBOOT__
CFLAGS += -D__CPU_ARCH__=$(CPU_ARCH)

ASFLAGS = $(CFLAGS) -D__ASSEMBLY__

#LDFLAGS = -Bstatic
#debug
#LDFLAGS = -pie

OBJCFLAGS = --gap-fill=0xff

export AS CC LD OBJCOPY OBJDUMP CFLAGS ASFLAGS LDFLAGS OBJCFLAGS 

CONFIG_SYS_TH_TEXT_BASE = 0
CONFIG_SYS_TH_PAD_TO = $(shell echo $$(($(CONFIG_SYS_TH_TEXT_BASE) + 4096)))

CONFIG_SYS_BH_TEXT_BASE = 0x30080000 

BUILT_IN_OBJ = built-in.o

CFLAGS += -DCONFIG_SYS_BH_TEXT_BASE=$(CONFIG_SYS_BH_TEXT_BASE)

export CONFIG_SYS_TH_TEXT_BASE CONFIG_SYS_TH_PAD_TO BUILT_IN_OBJ 

THDIR  = th
BH_SUBDIRS  = arch/$(ARCH)/$(CPU)/$(SOC)/
BH_SUBDIRS += arch/$(ARCH)/kernel/uCOSIII/
BH_SUBDIRS += arch/$(ARCH)/kernel/
BH_SUBDIRS += arch/$(ARCH)/lib/
BH_SUBDIRS += kernel/
BH_SUBDIRS += lib/
BH_SUBDIRS += drivers/tty/
BH_SUBDIRS += drivers/tty/serial/
BH_SUBDIRS += drivers/timer/
BH_SUBDIRS += kernel/uCOSIII/
 
SUBDIRS = $(THDIR) $(BH_SUBDIRS)
BH_OBJS = $(foreach n, $(BH_SUBDIRS), $(n)/$(BUILT_IN_OBJ))

.PHONY: init all th bh clean distclean depend $(BH_SUBDIRS)

all: init th bh

init:
	rm -f $(TOPDIR)/include/arch/cpu/soc/mach
	rm -f $(TOPDIR)/include/arch/cpu/soc
	rm -f $(TOPDIR)/include/arch/cpu
	rm -f $(TOPDIR)/include/arch
	ln -s arch-$(ARCH)  $(TOPDIR)/include/arch
	ln -s cpu-$(CPU)    $(TOPDIR)/include/arch/cpu
	ln -s soc-$(SOC)    $(TOPDIR)/include/arch/cpu/soc
	ln -s mach-$(BOARD) $(TOPDIR)/include/arch/cpu/soc/mach
	rm -f $(TOPDIR)/include/config.h
	echo "/* Automatically generated - do not edit */" | tee $(TOPDIR)/include/config.h
	echo "#ifndef _CONFIG_H" | tee -a $(TOPDIR)/include/config.h
	echo "#define _CONFIG_H" | tee -a $(TOPDIR)/include/config.h
	echo "#include <configs/${BOARD}.h>" | tee -a $(TOPDIR)/include/config.h
	echo "#endif /* _CONFIG_H */" | tee -a $(TOPDIR)/include/config.h

th: init
	make -C $(THDIR) all

bh: init mboot-bh.bin

mboot-bh.bin: mboot-bh.elf
	$(OBJCOPY) $(OBJCFLAGS) -O binary $< $@

mboot-bh.elf: $(BH_SUBDIRS) mboot-bh.lds start-bh.o
	$(LD) $(LDFLAGS) -Bstatic -T mboot-bh.lds $(BH_OBJS) -Ttext $(CONFIG_SYS_BH_TEXT_BASE) -Map mboot-bh.map -o $@
	$(OBJDUMP) -d $@ > mboot-bh.dis

mboot-bh.lds:
	rm -f $@
	ln -s $(TOPDIR)/arch/$(ARCH)/kernel/$@ $@	

start-bh.S:
	rm -f $@
	ln -s $(TOPDIR)/arch/$(ARCH)/kernel/$@ $@	

$(BH_SUBDIRS):
	make -C $@ all

clean:
	for dir in $(SUBDIRS); do \
		make -C $$dir clean; \
	done
	rm -f *.bin *.elf *.map *.dis *.o *.S
	rm -f mboot-bh.lds 
	find $(TOPDIR) -name "*~" -exec rm -f {} \;

distclean: clean
	rm -f $(TOPDIR)/include/arch/cpu/soc/mach
	rm -f $(TOPDIR)/include/arch/cpu/soc
	rm -f $(TOPDIR)/include/arch/cpu
	rm -f $(TOPDIR)/include/arch
	rm -f $(TOPDIR)/include/config.h
	
depend: init 
	for dir in $(SUBDIRS); do \
		make -C $$dir depend; \
	done

